class MyPromise {
	constructor(executor,name) {
		this.status = "pedding";
		this.value = ""; //记录通过时的参数
		this.reason = ""; //记录拒绝原因
		this.onResolvedCallbacks  = [];//异步时候的方法收集
		this.name = name;
		//存放失败回调的函数
		this.onRejectedCallbacks = []
		let resolve = (value) =>{
			if (this.status == "pedding") {
				this.value = value;
				this.status = "resolve";
				this.onResolvedCallbacks.map((item)=>{
					item(value);
				})
			}
		}
		let reject=(reason) =>{
			if(this.status == "pedding") {
				this.reason = reason;
				this.status = "reject";
				this.onRejectedCallbacks.map((item)=>{
					item(reason);
				})
			}
		}
		try {
			executor(resolve, reject);
		} catch (e) {
			//TODO handle the exception
			reject(e);
		}

	}
	then(onFufilled, onRejected) {
		onFufilled = typeof onFufilled == "function" ? onFufilled : v => v;
		onRejected = typeof onRejected == "function" ? onRejected : err => {
			throw err
		};
		let promise2;
		if (this.status == "resolve") {
			promise2 = new MyPromise((res,rej)=>{
				setTimeout(()=>{
					try{
						let x = onFufilled(this.value);
						this.resolvePromise(promise2,x,res,rej);
					}catch(e){
						//TODO handle the exception
						rej(e);
					}
				},0)
			})
		}
		if (this.status == "reject") {
			promise2 = new MyPromise((res,rej)=>{
				setTimeout(()=>{			
					let x = onRejected(this.reason);
					this.resolvePromise(promise2,x,res,rej);
				},0)
			})
		}
		if(this.status == "pedding"){
			promise2 = new MyPromise((res,rej)=>{
				this.onResolvedCallbacks.push((value)=>{
					setTimeout(()=>{
						try{
							let x = onFufilled(value);
							this.resolvePromise(promise2,x,res,rej);
						}catch(e){
							//TODO handle the exception
							rej(e)
						}
						
					},0)
				});
				this.onRejectedCallbacks.push((err)=>{
					setTimeout(()=>{
						let x = onRejected(err);
						this.resolvePromise(promise2,x,res,rej);
					},0)
				});
			})
			
		}
		return promise2;
	}
	resolvePromise(promise2,x,res,rej){
		if(promise2 == x){
			rej(new Error("qing bu yao fan hui xiang tong de promise"))
		}
		if(x != null && (typeof x == "function" || typeof x == "object")){
			let called = false;
			try{
				let then = x.then;
				if(typeof then == "function"){
					then.call(x,(y)=>{
						if(called)  return;
						called = true
						this.resolvePromise(promise2,y,res,rej);
					},function(err){
						if(called)  return;
						called = true
						rej(err);	
					})
				}else{
					res(x)	
				}
			}catch(e){
				if(called)  return;
				called = true
				//TODO handle the exception
				rej(e);
			}
		}else{
			res(x)
		}
		
	}
}
let p = new MyPromise(function(res, rej) {
	setTimeout(function(){
		res("123");
	},3000)
},"zhangsan").then(function(value) {
	console.log(value)
	return 36
}, function(err) {
	console.log(err)
}).then(function(v){
	console.log(v)
	//return 123214
	return new MyPromise(function(res,rej){
		setTimeout(function(){
			res(1243434)
		},2000)
	},"wanger")
},function(err){
	console.log(err)
}).then(function(value){
	console.log(value)
},function(err){
	console.log(err)
})




//a.js
/* let  b ;
  function f = new Promise(url,function(data){
	 b = data;
	 return b
 }))
 export default f;
 
 //c.js
 import f from 'a.js';
 
(async function(){
	let b = await f();
	console.log(b)
})() */